/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DYNAMIC_COMPONENT_EAWORKER_TASK_WRAPPER_H
#define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DYNAMIC_COMPONENT_EAWORKER_TASK_WRAPPER_H

#include "base/thread/task_executor.h"
#include <set>

namespace OHOS::Ace::NG {

class EaWorkerTaskWrapperImpl : public TaskWrapper,
    public std::enable_shared_from_this<EaWorkerTaskWrapperImpl> {
public:
    explicit EaWorkerTaskWrapperImpl(int32_t hostInstanceId, int32_t workerId);
    ~EaWorkerTaskWrapperImpl() override;
    bool WillRunOnCurrentThread() override;
    void Call(const TaskExecutor::Task& task,
        PriorityType priorityType = PriorityType::LOW) override;
    void Call(const TaskExecutor::Task& task, uint32_t delayTime,
        PriorityType priorityType = PriorityType::LOW) override;

    void SetCurrentPthread(const pthread_t& threadId);

    static void *GetCurrentAniEnv();

private:
    void DumpWorker();
    bool HasAttachCurrentThread(pthread_t tid);

    pthread_t threadId_ = 0;
    int32_t hostInstanceId_ = -1;
    int32_t workerId_ = -1;
    std::set<pthread_t> attachCurrentThreads_;
};

class EaWorkerEvent {
public:
    explicit EaWorkerEvent(const TaskExecutor::Task& task, int64_t ts) : task_(task), ts_(ts)
    {}

    void Fire()
    {
        task_();
    }

    int64_t GetTs()
    {
        return ts_;
    }

private:
    TaskExecutor::Task task_;
    int64_t ts_ = 0;
};
} // namespace OHOS::Ace::NG
#endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DYNAMIC_COMPONENT_EAWORKER_TASK_WRAPPER_H